package com.foxit.adapp;

//import java.io.*;

import java.util.ArrayList;
import java.util.Random;
import java.util.Arrays;

public class ActionAD {
     private String udid;
     private ArrayList<ActionUnit> au;
     private String[] tri;
     public ActionAD() {
     }
     public ActionAD(String udid, String actionset) {
           this.udid =udid;
           if(actionset != null && actionset.length()!=0) {
              this.au = new  ArrayList<ActionUnit>();
              String[] as= actionset.split(";");
              for(int i=0;i < as.length;i++) {
                   ActionUnit u = new ActionUnit(as[i]);
                   this.au.add(u);
              }
           }
     }
     public ActionAD(String udid, String actionset,String trace_info) {
           this.udid =udid;
           if(actionset != null && actionset.length()!=0) {
              this.au = new  ArrayList<ActionUnit>();
              String[] as= actionset.split(";");
              for(int i=0;i < as.length;i++) {
                   ActionUnit u = new ActionUnit(as[i]);
                   this.au.add(u);
              }
           }
           if(trace_info != null && trace_info.length()!=0) {
              this.tri = trace_info.split(",");
           }
     }
     public ArrayList<String[]> getAD(String trace_info,int v) {
          if(trace_info == null || trace_info.length()==0) return null;
          ArrayList<String[]> adl=new ArrayList<String[]>();
          String[] tr= trace_info.split(",");
          for(int j=0;j < tr.length;j++) {
             for(int i=0;i < au.size();i++) {
                if(au.get(i).getAction().indexOf(tr[j]) >=0) {
                    String [] a= new String[2];
                    a[0]=au.get(i).getAD(v);
                    a[1]=au.get(i).getDimension();
                    adl.add(a);
                }
             }
          }
          return adl;
     }
     public String[] getAD1() {
          if(tri == null) return null;
          ArrayList<String[]> adl=new ArrayList<String[]>();
          for(int j=0;j < tri.length;j++) {
             for(int i=0;i < au.size();i++) {
                if(au.get(i).getAction().indexOf(tri[j]) >=0) {
                   String [] a= new String[2];
                   a[0]=au.get(i).getAD(3);
                   a[1]=au.get(i).getDimension();
                   adl.add(a);
                }
             }
          }
          if(adl.size() <= 0) return null;
          Random rand = new Random();
          return adl.get(rand.nextInt(adl.size())); 
     }
     public ArrayList<String[]> getADS1() {
          if(tri == null) return null;
          ArrayList<String[]> adl=new ArrayList<String[]>();
          for(int j=0;j < tri.length;j++) {
             for(int i=0;i < au.size();i++) {
                if(au.get(i).getAction().indexOf(tri[j]) >=0) {
                   String[] l = au.get(i).getADS(3).split(",");
                   String [] a= new String[2];
                   for(int k=0;k < l.length;k++) {
                      a[0] = l[k];
                      a[1]=au.get(i).getDimension();
                      adl.add(a); 
                   }
                }
             }
          }
          return adl; 
     }
     public String[] getAD1(String udinfo) {
          if(tri!=null) {
             return getAD1();
          } 
          if(udinfo==null || udinfo.length()==0) return null;
          String[] u= udinfo.split(";");
//out.println("getAD1");
          ArrayList<String[]> adlist= new ArrayList<String[]>();
          for(int i=0; i < u.length;i++) {
             String[] a= u[i].split(",");
             adlist.add(a);
          } 
          ArrayList<String[]> adl=new ArrayList<String[]>();
          int x;
//out.println("getAD2");
          for(int j=0;j < adlist.size();j++) {
             x = Integer.valueOf(adlist.get(j)[0]);
//out.println("getAD3 x"+x);
             if(x <= 0) continue;
             if(x > 3) x=3;
             for(int i=0;i < au.size();i++) {
//out.println("au:"+au.get(i).getName()+" adlist:"+adlist.get(j)[1]);
                if(au.get(i).getName().indexOf(adlist.get(j)[1]) >=0) { 
                   String [] a= new String[2];
                   a[0]=au.get(i).getAD(x);
                   a[1]=au.get(i).getDimension();
                   adl.add(a); 
                }
             }
          }
          if(adl.size() <= 0) return null;
          Random rand = new Random();
          return adl.get(rand.nextInt(adl.size())); 
     }
     public ArrayList<String[]> getADS(String trace_info,int v) {
          if(trace_info == null || trace_info.length()==0) return null;
          ArrayList<String[]> adl=new ArrayList<String[]>();
          String[] tr= trace_info.split(",");
          for(int j=0;j < tr.length;j++) {
             for(int i=0;i < au.size();i++) {
                if(au.get(i).getAction().indexOf(tr[j]) >=0) {
                    String [] a= new String[2];
                    a[0]=au.get(i).getAD(v);
                    a[1]=au.get(i).getDimension();
                    adl.add(a);
                }
             }
          }
          return adl;
     }
     public ArrayList<String[]> getADS1(String udinfo,int maxno) {
          if(tri!=null) {
             return getADS1();
          } 
          if(udinfo==null || udinfo.length()==0) return null;
          String[] u= udinfo.split(";");
          ArrayList<String[]> adlist= new ArrayList<String[]>();
          for(int i=0; i < u.length;i++) {
             String[] a= u[i].split(",");
             adlist.add(a);
          } 
          ArrayList<String[]> adl=new ArrayList<String[]>();
          int x;
          if(maxno==0) maxno=999; 
          int m=0;
          for(int j=0;j < adlist.size();j++) {
             x = Integer.valueOf(adlist.get(j)[0]);
             if(x <= 0) continue;
             if(x > 3) x=3;
             for(int i=0;i < au.size();i++) {
//out.println("au:"+au.get(i).getName()+"  adlist:"+adlist.get(j)[1]);
                if(au.get(i).getName().indexOf(adlist.get(j)[1]) >=0) { 
                   String[] l = au.get(i).getADS(x).split(",");
                   String [] a= new String[2];
                   for(int k=0;k < l.length;k++) {
                      a[0] = l[k];
                      a[1]=au.get(i).getDimension();
//out.println("a: "+a[0]+ " dim:"+a[1]);
                      adl.add(a); 
                      if(++m >= maxno) break;
                   }
                }
                if(m >= maxno) break;
             }
          }
          return adl; 
     }
}
